package com.longxi.tianjintianbao.greedao.controller;


import android.database.sqlite.SQLiteDatabase;

import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.greedao.dao.DaoMaster;
import com.longxi.tianjintianbao.greedao.dao.DaoSession;
import com.longxi.tianjintianbao.greedao.dao.PatrolContentDao;
import com.longxi.tianjintianbao.greedao.dao.PatrolDao;
import com.longxi.tianjintianbao.greedao.entity.Patrol;
import com.longxi.tianjintianbao.greedao.entity.PatrolContent;
import com.longxi.tianjintianbao.utils.TimeComparisonUtils;

import java.sql.Time;
import java.util.Date;
import java.util.List;

/**
 * Created by 70339 on 2018/5/16.
 * 代码无八阿哥
 * 阿弥陀佛
 */
public class DataPatrolController {

    private static PatrolDao mPatrolDao = MyApplication.getInstance().getDaoSession().getPatrolDao();
    private static PatrolContentDao mPatrolContentDao = MyApplication.getInstance().getDaoSession().getPatrolContentDao();
    /**
     * 增加巡检项
     */
    public static long insertPatrolContent(Patrol patrol, PatrolContent patrolContent)
    {
        patrolContent.setBillid(patrol.getBillid());//增加巡检项表时要指定其父表（单据id和巡检表）
        patrolContent.setPatrol(patrol);
        return mPatrolContentDao.insertOrReplace(patrolContent);
    }
    /**
     * 增加巡检
     */
    public static long insertPatrol(Patrol patrol)
    {
        Patrol oldPatrol = mPatrolDao.queryBuilder()
                .where(PatrolDao.Properties.Billid.eq(patrol.getBillid()))
                .where(PatrolDao.Properties.Userid.eq(patrol.getUserid()))
                .where(PatrolDao.Properties.Iptype.eq(patrol.getIptype()))
                .unique();
        if(oldPatrol != null){
            return 0;
        }
        return mPatrolDao.insertOrReplace(patrol);
    }

    /**
     * 查巡检项
     */
    public static List<PatrolContent> SearchPatrolContent(Patrol patrol)
    {
        return patrol.getContent();//查找巡检项则需要通过巡检表来获取巡检项的集合然后再遍历找到所需巡检表
    }

    /**
     * 查巡检列表
     */
    public static List<Patrol> SearchPatrol(int page, int limit, String state)
    {
        Date nowDate = new Date();
        //惰性加载
        List<Patrol> list = mPatrolDao.queryBuilder()
                .where(PatrolDao.Properties.Userid.eq(MyApplication.loginUser.userid))
                .where(PatrolDao.Properties.Iptype.eq(MyApplication.IPID))
                .where(PatrolDao.Properties.IsDraft.eq(state))
                .offset(page * limit).limit(limit)
                .orderAsc(PatrolDao.Properties.Startdate)
                .listLazy();
        return list;
    }

    /**
     * 查巡检列表
     */
    public static List<Patrol> SelectByStartDate(int page, int limit, String state)
    {
        Date nowDate = new Date();
        //惰性加载
        List<Patrol> list = mPatrolDao.queryBuilder()
                .where(PatrolDao.Properties.Userid.eq(MyApplication.loginUser.userid))
                .where(PatrolDao.Properties.Iptype.eq(MyApplication.IPID))
                .where(PatrolDao.Properties.IsDraft.eq(state))
//                .where(PatrolDao.Properties.Startdate.lt(nowDate.getTime()))
                .where(PatrolDao.Properties.Enddate.gt(nowDate.getTime()))
                .offset(page * limit).limit(limit)
                .orderAsc(PatrolDao.Properties.Startdate)
                .listLazy();
        return list;
    }

    /**
     * 查巡检列表
     */
    public static List<Patrol> SelectByEnddatetime(int page, int limit, String state)
    {
        Date nowDate = new Date();
        //惰性加载
        List<Patrol> list = mPatrolDao.queryBuilder()
                .where(PatrolDao.Properties.Userid.eq(MyApplication.loginUser.userid))
                .where(PatrolDao.Properties.Iptype.eq(MyApplication.IPID))
                .where(PatrolDao.Properties.IsDraft.eq(state))
                .where(PatrolDao.Properties.Enddate.lt(nowDate.getTime()))
                .offset(page * limit).limit(limit)
                .listLazy();
        return list;
    }

    /**
     * 查巡检列表
     */
    public static List<Patrol> SelectByAddressid(long addressid, String state)
    {
        Date nowDate = new Date();
        //惰性加载
        List<Patrol> list = mPatrolDao.queryBuilder()
                .where(PatrolDao.Properties.Userid.eq(MyApplication.loginUser.userid))
                .where(PatrolDao.Properties.Iptype.eq(MyApplication.IPID))
                .where(PatrolDao.Properties.IsDraft.eq(state))
//                .where(PatrolDao.Properties.Startdate.ge(TimeComparisonUtils.getStartTime()))
                .where(PatrolDao.Properties.Startdate.lt(nowDate.getTime()))
                .where(PatrolDao.Properties.Enddate.ge(nowDate.getTime()))
                .where(PatrolDao.Properties.Addressid.eq(addressid))
                .orderAsc(PatrolDao.Properties.Startdate)
                .listLazy();
        return list;
    }

    /**
     * 根据Billid获取Patrol详情
     * @param Billid
     * @return
     */
    public static Patrol getPatrolByBillid(long Billid){
        Patrol patrol = mPatrolDao.queryBuilder()
                .where(PatrolDao.Properties.Userid.eq(MyApplication.loginUser.userid))
                .where(PatrolDao.Properties.Iptype.eq(MyApplication.IPID))
                .where(PatrolDao.Properties.Billid.eq(Billid))
                .unique();
        return patrol;
    }

    /**
     * 判断该用户是否有需要上传的数据
     * @return
     */
    public static long isUploadData(){
        long count = mPatrolDao.queryBuilder()
                .where(PatrolDao.Properties.Userid.eq(MyApplication.loginUser.userid))
                .where(PatrolDao.Properties.Iptype.eq(MyApplication.IPID))
                .where(PatrolDao.Properties.IsDraft.eq("1"))
                .count();
        return count;
    }

    /**
     * 获取需要上传的数据
     * @return
     */
    public static List<Patrol> getUploadList(){
        List<Patrol> list = mPatrolDao.queryBuilder()
                .where(PatrolDao.Properties.Userid.eq(MyApplication.loginUser.userid))
                .where(PatrolDao.Properties.Iptype.eq(MyApplication.IPID))
                .where(PatrolDao.Properties.IsDraft.eq("1"))
                .list();
        return list;
    }

    /**
     * 删除某条巡检信息
     * @param i 删除数据的id
     */
    public static void deletePatrol(long i)
    {
        mPatrolDao.deleteByKey(i);
        //当然Greendao还提供了其他的删除方法，只是传值不同而已
    }

    /**
     *修改某条巡检项
     */
    public static void correctSportInfo(PatrolContent info)
    {
        mPatrolContentDao.update(info);
    }

    public static void updateAllByIsDraft(){
//        mPatrolDao.queryBuilder()
//                .where(PatrolDao.Properties.Userid.eq(MyApplication.loginUser.userid))
//                .where(PatrolDao.Properties.Iptype.eq(MyApplication.IPID))
//                .where(PatrolDao.Properties.IsDraft.eq("1"))
//                .update();
        String sql ="update PATROL set IS_DRAFT = '2'" +
                " where USERID = " + MyApplication.loginUser.userid
                + " and IPTYPE = " + MyApplication.IPID
                + " and IS_DRAFT = '1'" ;
        MyApplication.db.execSQL(sql);
    }

    public static void updateIsDraftById(Patrol patrol){
        mPatrolDao.update(patrol);
    }

    /**
     *修改某条巡检信息
     */
    public static void correctPatrol(Patrol info)
    {
        mPatrolDao.update(info);
    }
}
